package day5.HomeWork;

import java.io.*;
import java.util.Properties;

/**
 * ClassName：Test3
 *
 * @Author：小鱼
 * @Create：2025-08-22 17:25
 */
public class Test3 {
    /*
    3.通过Properties类加载db.properties文件，获取文件内容并输出，然后修改配置并写入。
      db.properties
    */
    public static void main(String[] args) throws Exception{
        //通过Properties类加载db.properties文件，获取文件内容并输出
        Properties p = new Properties();
        //准备输入流对象
        File file = new File("D:\\IDEA_project\\JP_Java\\week6\\src\\day5\\HomeWork\\db.properties");
        InputStream in = new FileInputStream(file);
        //加载
        p.load(in);
        //获取
        String driver = p.getProperty("driver");
        String url = p.getProperty("url");
        String username = p.getProperty("username");
        String password = p.getProperty("password");
        //输出
        System.out.println("driver: " + driver);
        System.out.println("url: " + url);
        System.out.println("username: " + username);
        System.out.println("password: " + password);
        //关流
        in.close();

        //修改配置并写入db.properties
        p.setProperty("username", "root");
        p.setProperty("password", "root");
        //输出流
        OutputStream os = new FileOutputStream(file);
        p.store(os, "this is comment msg");
        System.out.println("修改配置完成!");
        //关流
        os.close();


    }
}
